
var viewLocationContainer = Ti.UI.createView( {
//	backgroundColor : viewport_bg,
//	opacity : 0.30,
//	borderRadius : 10,
//	borderColor : viewport_border,
//	borderWidth : 2,
	width : 280,
	height : 116,
	top : 10,
	left : 20,
	layout : 'vertical'
});

var distanceLabel = Ti.UI.createLabel( {
	color : '#FFFFFF',
	text : 'Distance',
	top : 5,
	left : 10,
	width : 'auto',
	height : 'auto'
});
viewLocationContainer.add(distanceLabel);

var searchResults = {
	type : 'location',
	distance : 50,
	// The location defaults to DotNetNuke office in San Mateo
	currentLocation: {
		lat: '37.549528',
		lng: '-122.316826'
	},
	usergroups : []
};

var customDistanceData = [ 
   {label : "10 mi.", hasChild : false}, 
   {label : "25 mi.", hasChild : false}, 
   {label : "50 mi.", hasChild : false}, 
   {label : "100 mi.", hasChild : false, selected : true },
   {label : "250 mi.", hasChild : false} /*, 
   {label : "500 mi.", hasChild : false},
   {label : "1000 mi.", hasChild : false} */
];

var selectedDistance = DotNetNuke.getSelectedDataRow(customDistanceData);

var distancePicker = DotNetNuke.createPicker( {
	top : 5,
	left : 10,
	right : 10,
	height : 34,
	editable : true,
	hintText : selectedDistance.label,
	borderColor : '#700',
	borderWidth : 1
});

var distanceSelector;

distancePicker.addEventListener('click', function(e) {
	var popup = Ti.UI.createWindow( {
		//modal : true,
		navBarHidden : true
	});

	distanceSelector = DotNetNuke.createSelector( {
		width : 290,
		height : 300,
		data : customDistanceData
	});

	distanceSelector.addEventListener('click', function(e) {
		popup.close();
		distancePicker.fireEvent('setText', {
			text : customDistanceData[e.index].label
		});
		DotNetNuke.selectDataRow(customDistanceData, e.index);
	});

	popup.add(distanceSelector);
	popup.open();
});
viewLocationContainer.add(distancePicker);

var searchLocation = Ti.UI.createButton( {
	title : 'Search by Location',
	top : 10,
	right : 10,
	height : 35,
	width : 150
});
viewLocationContainer.add(searchLocation);

var longitude = 0.0;
var latitude = 0.0;

Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
Ti.Geolocation.distanceFilter = 10;

Ti.Geolocation.getCurrentPosition(function(e){
	Ti.API.info('GetCurrentPosition: ' + JSON.stringify(e));
	if (e.error) {
		//alert('error ' + JSON.stringify(e.error));
		searchLocation.enabled = false;
		return;
	}

	searchLocation.enabled = true;
	searchResults.currentLocation.lng  = e.coords.longitude;
	searchResults.currentLocation.lat = e.coords.latitude;
});

searchLocation.addEventListener('click', function(e) {
	var xhr = Ti.Network.createHTTPClient();

	xhr.onload = function() {
		// We want to make sure that we are getting the expected JSON response and not HTML
		// which would indicate some kind of error.
		if (this.responseText.match(/"d":\[/)) {
			searchResults.usergroups = JSON.parse(this.responseText).d;

			Ti.App.Properties.setString("ug_search", JSON.stringify(searchResults));
			var ugsearch = Ti.UI.createWindow( {
				//modal : true,
			    backgroundImage:DotNetNuke.getBackgroundImage(),
			    navBarHidden: true,
			    orientationModes: Ti.UI.PORTRAIT,
			    url : 'searchresults.js'
			});
			hideLoading();
			ugsearch.open();
		}
		else {
			alert('The server is playing hard to get and won\'t talk to me!  Please try again later.');
		}
	};

	xhr.onerror = function(e) {
		alert('The server is currently on a lunch break!  Please try again later.');
		hideLoading();
	};

	// 'http://10.0.2.2/WebServiceTest/SampleService.asmx' +
	
	showLoading();
	var SearchByLocationUrl =  constants.serviceBaseUrl + constants.SearchByLocation;
	xhr.open('POST', SearchByLocationUrl);

	xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
	xhr.setRequestHeader('Accept', 'application/json');
	
	var distance = DotNetNuke.getSelectedDataRow(customDistanceData).label;
	searchResults.distance = distance.replace(' mi.', '');
	
	var data = "{ 'lati': '" + searchResults.currentLocation.lat + "', 'longi': '" + searchResults.currentLocation.lng + "', 'distance': '" + searchResults.distance + "', 'inKilometers' : 'false' }";
	
	xhr.send(data);

});

searchPanel.add(viewLocationContainer);
